axios.defaults.withCredentials = true;
// axios.defaults.baseURL = 'http://172.28.83.3:9999/law/';
 axios.defaults.baseURL = 'http://192.168.0.150:9999/law/';//测试环境
// axios.defaults.baseURL = 'http://10.50.141.246:8090/law/';//正式环境



// http 请求封装
Vue.prototype.$HttpUtil = (options, successCallback, errCallback) => {
    if (options.method === 'get' || options.method === 'GET' || !options.method) {
        let params = ''
        if (options.data === undefined) {
            options.url = options.url
        } else {
            for (let key in options.data) {
                params += key + '=' + options.data[key] + '&'
            }
            params = params.substring(params.length - 1, 0)
            options.url = options.url + '?' + params
        }
    }
    // let j = request.jar()
    // let httpOptions = {
    //   url: urls.baseUrl + options.url,
    //   method: options.method ? options.method : 'GET',
    //   json: true,
    //   headers: {
    //     'content-type': 'application/json'
    //   },
    //   jar: j,
    //   body: options.data ? options.data : {}
    // }
    // function callback(error, response, body) {
    //   if (!error && response.statusCode === 200) {
    //     console.log(response)
    //
    //     console.log(body)
    //   }
    // }
    // request(httpOptions, callback)

    axios({
        method: options.method ? options.method : 'GET',
        url: options.url,
        data: options.data ? options.data : {}
    }).then(function (res) {
        console.log(res)
        if (res.data.code === -999) {
            Vue.prototype.$notify({
                title: '失败',
                message: res.data.msg,
                type: 'error',
                duration: 0
            })
            router.push('/')
        }
        let data = res.data
        if (successCallback) {
            successCallback(data)
        }
    }).catch(function (err) {
        if (errCallback) {
            errCallback(err)
        }
    })
}
Vue.prototype.$NotifyUtil = (options) => {
    Vue.prototype.$notify({
      title: options.title ? options.title : '',
      message: options.message,
      type: options.type ? options.type : 'success',
      duration: options.duration || options.duration === 0 ? options.duration : 2000
    })
  }
  // title提升标题 msg提示信息 type提示类型 duration 消息停留时间
  // const NotifyUtil = (title, msg, type, duration) => {
  //   Vue.prototype.$notify({
  //     title: title || '成功',
  //     message: msg,
  //     type: type || 'success',
  //     duration: duration || 2000
  //   })
  // }
  const Refresh = (options, successCallback) => {
    let optionss = {
      method: 'post',
      url: 'biz/flow/updateFlowState.do',
      data: {
        flowId: options.flowId,
        stateNum: options.stateNum
      }
    }
    HttpUtil(
      optionss,
      res => {
        if (successCallback) {
          successCallback(res.data)
        }
      },
      err => {
        NotifyUtil({title: '失败', type: 'error', msg: err})
      }
    )
  }
  
  const GetIdCardInfo = (successCallback, errCallback) => {
    axios({
      method: 'GET',
      url: urls.idacardUrl
    }).then(function (res) {
      let data = res.data
      if (successCallback) {
        successCallback(data)
      }
    }).catch(function (err) {
      if (errCallback) {
        errCallback(err)
      }
    })
  }
  
  const UploadFile = (file, successCallback, errCallback) => {
    let param = new FormData()  // 创建form对象
    param.append('file', file)  // 通过append向form对象添加数据
    axios({
      method: 'POST',
      url: urls.upLoadFileUrl,
      data: param
    }).then(function (res) {
      let data = res.data
      if (successCallback) {
        successCallback(data)
      }
    }).catch(function (err) {
      if (errCallback) {
        errCallback(err)
      }
    })
  }
  
  const typeObj = (obj) => {
    var toString = Object.prototype.toString
    var map = {
      '[object Boolean]': 'boolean',
      '[object Number]': 'number',
      '[object String]': 'string',
      '[object Function]': 'function',
      '[object Array]': 'array',
      '[object Date]': 'date',
      '[object RegExp]': 'regExp',
      '[object Undefined]': 'undefined',
      '[object Null]': 'null',
      '[object Object]': 'object'
    }
    return map[toString.call(obj)]
  }
  const deepCopy = (data) => {
    let t = typeObj(data)
    let o
    let i
    let ni
  
    if (t === 'array') {
      o = []
    } else if (t === 'object') {
      o = {}
    } else {
      return data
    }
  
    if (t === 'array') {
      for (i = 0, ni = data.length; i < ni; i++) {
        o.push(deepCopy(data[i]))
      }
      return o
    } else if (t === 'object') {
      for (i in data) {
        o[i] = deepCopy(data[i])
      }
      return o
    }
  }
  const Logout = () => {
    let options = {
      url: 'sys/sysUser/loginOut',
      method: 'POST'
    }
    HttpUtil(options, res => {
      if (res.code === 0) {
        NotifyUtil({message: res.msg})
        router.push('/login')
      }
    })
  }
  
  const convertBase64UrlToBlob = (urlData, type) => {
    var bytes = window.atob(urlData.split(',')[1])
    var ab = new ArrayBuffer(bytes.length)
    var ia = new Uint8Array(ab)
    for (var i = 0; i < bytes.length; i++) {
      ia[i] = bytes.charCodeAt(i)
    }
    return new Blob([ab], {type: 'image/' + type})
  }
  const dataURItoBlob = (base64Data) => {
    var byteString
    if (base64Data.split(',')[0].indexOf('base64') >= 0) {
      byteString = atob(base64Data.split(',')[1])
    } else {
      byteString = unescape(base64Data.split(',')[1])
    }
    var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0]
    var ia = new Uint8Array(byteString.length)
    for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i)
    }
    return new Blob([ia], {type: mimeString})
  }

function mGetDate(){
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var d = new Date(year, month, 0);
    return d.getDate();
}
function convertDateFromString(dateString) {
    if (dateString) {
        var date = new Date(dateString.replace(/-/,"/"))
        return date;
    }
}


  